package com.dongge.action;

import com.dongge.annotation.IsLogin;
import com.dongge.entity.Information;
import com.dongge.entity.User;
import com.dongge.service.InformationService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * Created by dongge on 16/7/31.
 */
@Controller
@Scope("prototype")
public class InformationAction extends ActionSupport implements ServletContextAware {
    private long id;
    private String content;
    private User user;
    //用户id应该从session中获取
    private ServletContext servletContext;

    public InformationAction() {
//        WebApplicationContextUtils.getWebApplicationContext()
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        user = (User)session.getAttribute("user");
    }

    @Autowired
    private InformationService informationService;

    public void setServletContext(ServletContext context) {
        servletContext = context;
    }

    /**
     * 只有信息的创建者或者管理员才有资格删除这条信息
     * @return
     */
    /**
     * 这里使用aop可能更好
     * @return
     */
    @IsLogin
    public String delete(){
//        ActionContext.getContext().get
//        SecurityUtils.getSubject().getSession().
        informationService.delete(id);
        return "list";
    }

    /**
     *
     * @return
     */
    @IsLogin
    public String edit(){
        Information information = informationService.get(id);
        ActionContext.getContext().getValueStack().push(information);
        return "edit";
    }

    @IsLogin
    public String add(){
        return "add";
    }

    @IsLogin
    public String create(){
        informationService.save(content,user);
        return "list";
    }

    @IsLogin
    private String update(){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User)session.getAttribute("user");
        informationService.update(id,content,user);
        return "list";
    }

    public String list(){
        System.out.println("list is called");
        List<Information> informations = informationService.getInformations();
        ActionContext.getContext().put("informations",informations);
        return SUCCESS;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
